package com.liy.framework.common.tree;

import com.liy.framework.common.utils.ReflectUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TreeUtil<T> {
    private List<TreeNode> treeNodeList = new ArrayList();
    private List<T> data;
    private String rootValue = "0";
    private List<TreeNode> tempData = null;
    private List<TreeNode> nodeData = null;
    private TreeNodeMapping mapping;

    public TreeUtil(List<T> data, String rootValue) {
        this.data = data;
        this.rootValue = rootValue;
        this.mapping = new TreeNodeMapping();
    }

    public TreeUtil(List<T> data, String rootValue, TreeNodeMapping mapping) {
        this.data = data;
        this.rootValue = rootValue;
        this.mapping = mapping != null ? mapping : new TreeNodeMapping();
    }

    public List<TreeNode> processToList() {
        if (this.data == null && this.data.size() <= 0) {
            return this.treeNodeList;
        } else {
            List<TreeNode> rootNode = this.findRootList(this.rootValue);
            this.nodeData.removeAll(rootNode);
            Iterator var2 = rootNode.iterator();

            while(var2.hasNext()) {
                TreeNode node = (TreeNode)var2.next();
                this.tempData = new ArrayList();
                this.eachRootNode(node, this.nodeData);
                this.nodeData.removeAll(this.tempData);
                this.treeNodeList.add(node);
            }

            return this.treeNodeList;
        }
    }

    public TreeNode processToOneNode(String name) {
        List<TreeNode> list = this.processToList();
        TreeNode node = new TreeNode();
        node.setId("");
        node.setPid("");
        node.setName(name);
        node.setIcon("");
        node.setChildren(list);
        return node;
    }

    private List<TreeNode> findRootList(String rootValue) {
        List<TreeNode> rootList = new ArrayList();
        this.nodeData = new ArrayList();

        TreeNode node;
        for(Iterator var3 = this.data.iterator(); var3.hasNext(); this.nodeData.add(node)) {
            T t = (T) var3.next();
            node = this.getTreeNode(t);
            if (rootValue.equals(node.getPid())) {
                rootList.add(node);
            }
        }

        return rootList;
    }

    private void eachRootNode(TreeNode parentNode, List<TreeNode> itemList) {
        List<TreeNode> childrenList = new ArrayList();
        Iterator var4 = itemList.iterator();

        while(var4.hasNext()) {
            TreeNode node = (TreeNode)var4.next();
            if (parentNode.getId().equals(node.getPid())) {
                this.tempData.add(node);
                this.eachRootNode(node, itemList);
                childrenList.add(node);
            }
        }

        parentNode.setChildren(childrenList);
    }

    private TreeNode getTreeNode(T t) {
        String id = (String) ReflectUtil.getFieldValue(this.mapping.getId(), t);
        String pid = (String)ReflectUtil.getFieldValue(this.mapping.getPid(), t);
        String name = (String)ReflectUtil.getFieldValue(this.mapping.getName(), t);
        String icon = (String)ReflectUtil.getFieldValue(this.mapping.getIcon(), t);
        TreeNode node = new TreeNode();
        node.setId(id);
        node.setPid(pid);
        node.setName(name);
        node.setIcon(icon);
        if (this.mapping.getAttributes() != null) {
            Map<String, Object> attributeMap = new HashMap();
            String[] var8 = this.mapping.getAttributes();
            int var9 = var8.length;

            for(int var10 = 0; var10 < var9; ++var10) {
                String key = var8[var10];
                Object val = ReflectUtil.getFieldValue(key, t);
                attributeMap.put(key, val);
            }

            node.setAttributes(attributeMap);
        }

        return node;
    }
}
